Skip to content

Conversation

@alexanderivrii
Copy link
Collaborator

@alexanderivrii alexanderivrii commented Nov 12, 2025

Addresses #17 by implementing a function (check_solution) that checks whether the unitary operator for the synthesized sequence of gates is sufficiently close to the desired unitary operator for the rotation gate.


Update:

As per @jlapeyre's comment, it is in fact not too difficult to directly compute the operator norm for a 2x2 matrix. Letting
$A = R_z(\theta) - U$, with

$$ R_z(\theta) = \begin{pmatrix} x & 0 \\ 0 & x^* \end{pmatrix} $$

$$ U = \begin{pmatrix} u & -t^* \\ t & u^* \end{pmatrix} $$

(where $x = e^{-i \theta / 2}$), the eigenvalues of $A^* A$ can be computed as $\lambda = 2 - (x^* u + u^* x)$, and the operator norm is given as $\sqrt \lambda$. This is close but still a bit different from the formula in pygridsynth.


@jlapeyre, one question: what is the best way to handle the result? I was not sure if we should raise an error or handle the result in some other way, so currently it just print to the screen whether the solution is correct or not. But we want something better (in particular for adding a few integration tests).

A new argument check_solution to GridSynthConfig, which can be instantiated from command line by specifying --check and from external code by specifying config_from_theta_epsilon(...).with_check_solution(true). Btw, this is a nice pattern that avoids updating the calling code when new arguments are added to GridSynthConfig.

@jlapeyre
Copy link
Collaborator

I think the Frobenius norm might be ok. But the algorithm uses the operator norm when choosing the appropriate exactly syntehsizable unitary. (I don't recall exactly where this code occurs) In case the distance is just under the desired $\epsilon$, your check might fail.

The operator norm (with respect to the Euclidean vector norm, which is what the paper intends) is equal to the square root of the largest singular value. This is easy to compute for a 2x2 matrix. You mentioned this out of band: I recall looking at the pygrisynth code in the past and deciding that this is what it uses to report the error.

@jlapeyre
Copy link
Collaborator

jlapeyre commented Nov 13, 2025

from external code by specifying config_from_theta_epsilon(...).with_check_solution(true)

So, if you include more options, then you just pile on more of these like with_opt_a(valuea).with_opt_b(valueb), etc. ?

I like this because it doesn't break existing code that depends on the library. And it allows you to get around Rust's disapproval of default values.

@jlapeyre
Copy link
Collaborator

what is the best way to handle the result?

I would calculate the error and have the library call return it somehow with the result. This is what numerical libraries tend to do. gridsynth and pygridsynth do this, I think.

If the algorithm is implemented correctly, the calculated error should always be within the tolerance. I normally associate the idea of a user checking the error with algorithms that have various stopping criteria and behave in ways that are not easy to predict before doing the calculation.

In this case, what I had in mind was checking the error in the test suite; to ensure that the implementation remains correct. That would involve making a library call that returns the solution and the error, and then checking that the error is within the specified tolerance.

Of course, in the test suite, a test should fail if the error is too large.

@alexanderivrii
Copy link
Collaborator Author

Thanks @jlapeyre for the feedback. I have reimplemented the norm to be the true operator norm. To return the result, I have added a new structure GridSynthResult, which now contains both gates and the result of the check; we will also need this structure when implementing "up-to-phase", since we will need to return whether the global phase needs to be adjust or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants